/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.articles.importFromLn.updates;

import cz.mzk.kramerius.commons.Dom4jUtils;
import cz.mzk.kramerius.commons.Namespaces;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.QName;
import org.dom4j.XPath;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class RelsExtModelUpdater implements DatastreamUpdater {

    private static final XPath hasModelXpath = Dom4jUtils.createXPath("rdf:RDF/rdf:Description/fedora-model:hasModel");
    private static final XPath descriptionXpath = Dom4jUtils.createXPath("rdf:RDF/rdf:Description");
    public static final String NEW_MODEL_VALUE = "info:fedora/model:article";

    @Override
    public Document update(Document doc) {
        Node old = hasModelXpath.selectSingleNode(doc);
        old.detach();
        Element description = (Element) descriptionXpath.selectSingleNode(doc);
        Element hasModel = description.addElement(new QName("hasModel", Namespaces.fedora_model));
        hasModel.addAttribute(new QName("resource", Namespaces.rdf), NEW_MODEL_VALUE);
        return doc;
    }
}
